# %matplotlib inline
from nuscenes.nuscenes import NuScenes

#初始化
nusc = NuScenes(version='v1.0-mini', dataroot='/home/yyj/dl_dataset/nuscenes', verbose=True)

#!nuscenes 数据集每个scene 是一段20s的场景
nusc.list_scenes()
my_scene = nusc.scene[0]

# scene metadata 
# print(my_scene)

#! Sample 
# an annotated keyframe of a scene at a given timestamp 
first_sample_token = my_scene["first_sample_token"]
last_sample_token = my_scene["last_sample_token"]

#渲染sample  可视化各个传感器数据以及annotation 
# nusc.render_sample(first_sample_token)
# nusc.render_sample(last_sample_token) # 将本次采样时间的所有数据都展示出来
#exmine its metadata 
my_sample = nusc.get('sample',first_sample_token)
# print(my_sample)

#list 
#会打印本次sample 所有的传感器数据以annotation 
# nusc.list_sample(my_sample['token'])


#! sample data
# print(my_sample['data'])
sensor = 'CAM_FRONT'
cam_front_data = nusc.get('sample_data', my_sample['data'][sensor])
print(cam_front_data['calibrated_sensor_token'])
calibrated_sensor_token = nusc.get('calibrated_sensor',cam_front_data['calibrated_sensor_token'])
#render 会将原始数据以及标注同时标出 
# nusc.render_sample_data(cam_front_data['token']) #只展示当前传感器的数据
print(calibrated_sensor_token)

#! sample annotation :是指场景中的bounding box 
my_annotation_token = my_sample['anns'][1]
my_annotation_metadata =  nusc.get('sample_annotation', my_annotation_token)
# print(my_annotation_metadata)
# nusc.render_annotation(my_annotation_token)


#! instance : 场景中需要被感知的物体如行人、车辆
instance_nums = len(nusc.instance)
# print(instance_nums) # 911
my_instance = nusc.instance[100]
# print(my_instance)
instance_token = my_instance['token']
# nusc.render_instance(instance_token)



print("Last annotated sample of this instance")
# nusc.render_annotation(my_instance['last_annotation_token'])


#! category 
# nusc.list_categories() #物体标注的一个类别
# print(nusc.category[9])

#! Attribute 
nusc.list_attributes()

#! 一个展示attribute是怎样一步步在场景中进行变化的例子 
my_instance = nusc.instance[27]
first_token = my_instance['first_annotation_token']
last_token = my_instance['last_annotation_token']
nbr_samples = my_instance['nbr_annotations']
current_token = first_token

i = 0
found_change = False
while current_token != last_token:
    current_ann = nusc.get('sample_annotation', current_token)
    current_attr = nusc.get('attribute', current_ann['attribute_tokens'][0])['name']
    
    if i == 0:
        pass
    elif current_attr != last_attr:
        print("Changed from `{}` to `{}` at timestamp {} out of {} annotated timestamps".format(last_attr, current_attr, i, nbr_samples))
        found_change = True

    next_token = current_ann['next'] #类似于链表的形式
    current_token = next_token
    last_attr = current_attr
    i += 1
    
#! visibility 
#将全部6个相机中的可见度分为了四个档次
# print(nusc.visibility)

#看一个可见度在80-100的示例
anntoken = 'a7d0722bce164f88adf03ada491ea0ba'
visibility_token = nusc.get('sample_annotation', anntoken)['visibility_token']

# print("Visibility: {}".format(nusc.get('visibility', visibility_token)))
nusc.render_annotation(anntoken)

# print(nusc.sensor)

#! nuScenes Basics 
# print(nusc.category[0])
cat_token = nusc.category[0]['token']
# print(cat_token)
# print(nusc.get('category',cat_token)) #底层是通过getattr构成 用于从class中获取
#! 看看sample_annotation 
# print(nusc.sample_annotation[0])

# nusc.get('visibility',nusc.sample_annotation[0]('visibility_token'))
# nusc.list_categories() #会打印出所有类别 ， 以及统计的尺寸大小
# nusc.list_attributes() #lists all attributes and counts.
# nusc.list_scenes()

my_sample = nusc.sample[10]
# print(my_sample['data']['LIDAR_TOP'])
lidar_data = nusc.get('sample_data', my_sample['data']['LIDAR_TOP'])
# print(lidar_data['ego_pose_token'])
lidar_pose = nusc.get('ego_pose',lidar_data['ego_pose_token'])
# print("lidar pose : {}".format(lidar_pose))
# nusc.render_pointcloud_in_image(my_sample['token'],pointsensor_channel='LIDAR_TOP')
# nusc.render_pointcloud_in_image(my_sample['token'], pointsensor_channel='LIDAR_TOP', render_intensity=True)


# nusc.render_pointcloud_in_image(my_sample['token'], pointsensor_channel='RADAR_FRONT')


# my_sample = nusc.sample[20]
# nusc.render_sample(my_sample['token'])



